package com.example.baserecyclerviewadapterdemo.activity.multi;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseBinderAdapter;
import com.chad.library.adapter.base.binder.QuickItemBinder;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.baserecyclerviewadapterdemo.R;
import com.example.baserecyclerviewadapterdemo.entity.MultiItemAdapterEntity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class MultiItemBinderActivity extends AppCompatActivity {
    private BaseBinderAdapter adapter = new BaseBinderAdapter();
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_item_binder);

        final Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        recyclerView = findViewById(R.id.rv_list);
        initRecycler();
    }

    private void initRecycler(){
        adapter.addItemBinder(ImageItemBinder.ImageEntity.class,new ImageItemBinder());
        adapter.addItemBinder(MultiItemAdapterEntity.class,new MultiItemEntityBinder());
        adapter.addItemBinder(HeaderImageEntityBinder.HeaderEntity.class,new HeaderImageEntityBinder());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    private final static class ImageItemBinder extends QuickItemBinder<ImageItemBinder.ImageEntity>{

        @Override
        public int getLayoutId() {
            return R.layout.item_image_view;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, ImageEntity imageEntity) {
            if (imageEntity.getImageUrl() != null) {
                ImageView iv = holder.getView(R.id.iv);
                Glide.with(getContext()).load(imageEntity.getImageUrl()).into(iv);
            }
        }

        public static class ImageEntity {
            private String imageUrl;

            public String getImageUrl() {
                return imageUrl;
            }

            public void setImageUrl(String imageUrl) {
                this.imageUrl = imageUrl;
            }

            public ImageEntity() {
            }

            public ImageEntity(String imageUrl) {
                this.imageUrl = imageUrl;
            }
        }
    }
    private final static class MultiItemEntityBinder extends QuickItemBinder<MultiItemAdapterEntity>{

        @Override
        public int getLayoutId() {
            return R.layout.adapter_multi_item;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, MultiItemAdapterEntity item) {
            holder.setText(R.id.tv_title,item.getTitle());
            holder.setText(R.id.tv_content,item.getContent());
            ImageView imageView = holder.getView(R.id.imageView);
            Glide.with(getContext()).load(item.getImageUrl()).into(imageView);
        }
    }
    private final static class HeaderImageEntityBinder extends QuickItemBinder<HeaderImageEntityBinder.HeaderEntity>{

        @Override
        public int getLayoutId() {
            return R.layout.adapter_header_and_footer;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, HeaderImageEntityBinder.HeaderEntity headerEntity) {
            ImageView imageView = holder.getView(R.id.iv);
            Glide.with(getContext()).load(headerEntity.getImageUrl()).into(imageView);
        }

        public static class HeaderEntity {
            private String imageUrl;

            public String getImageUrl() {
                return imageUrl;
            }

            public void setImageUrl(String imageUrl) {
                this.imageUrl = imageUrl;
            }

            public HeaderEntity(String imageUrl) {
                this.imageUrl = imageUrl;
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        List<Object> data = new ArrayList<>();
        data.add(new ImageItemBinder.ImageEntity());
        data.add(new ImageItemBinder.ImageEntity("https://static.oschina.net/uploads/img/202008/31180731_vRmf.png"));
        data.add(new MultiItemAdapterEntity("https://static.oschina.net/uploads/space/2021/0304/081357_xZj5_2720166.png",
                "重磅！谷歌发布 Flutter 2","Flutter 2 是 Flutter 的重大升级版本，可帮助开发者为任何平台创建美观、快速和可移植的应用程序。谷歌表示，开发者借助 Flutter 2 可使用同一份代码库面向 5 个操作系统（iOS, Android, Windows, macOS 和 Linux）发布原生应用，以及发布面向 Chrome, Firefox, Safari 和 Edge 等浏览器的应用。此外，Flutter 还可以嵌入到汽车、电视和智能家电中。"));
        data.add(new MultiItemAdapterEntity("https://static.oschina.net/uploads/cooperation/index_channel_list_ad_between_gitee_QAVZt.jpg",
                "云+社区技术沙龙「云原生」专场","云原生在近几年的发展越来越火热，作为云上最佳实践而生的设计理念，也有了越来越多的实践案例：对资源的极致利用，对系统的精细把控，对效能的有力提升，对云边端的一体追求"));
        data.add(new HeaderImageEntityBinder.HeaderEntity("https://static.oschina.net/uploads/cooperation/news_detail_right_sidebar_3_DVmQY.png"));
        adapter.setList(data);
    }
}
